﻿using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductScreenSPsSnapshootService
    {
        /// <summary>
        /// 创建可用配件快照
        /// </summary>
        /// <param name="insuranceOrderId">保单表InsuranceOrder主键ID</param>
        /// <param name="serviceProjectType">保险信息类型：和ServiceProductType是同一个枚举，根据ServiceProjectType去不同的表取数据，比如手机维修保险信息，就去CellPhoneInsuranceInfo表取</param>
        /// <returns></returns>
        public bool CreateSnapshoot(long insuranceOrderId, ServiceProductType serviceProjectType)
        {
            var result = true;
            var cellPhoneInsuranceInfoList = ServiceFactory.CellPhoneInsuranceInfoServiceInstance.LoadByInsuranceID(insuranceOrderId);//todo 根据ServiceProjectType去不同的表取数据
            if (cellPhoneInsuranceInfoList.Any())
            {
                var cellPhoneServiceProjectIDs =
                    cellPhoneInsuranceInfoList.Select(o => o.CellPhoneServiceProjectID.Value).ToList();
                var insuranceProductScreenSPs =
                    ServiceFactory.InsuranceProductScreenSPsServiceInstance.GetInsuranceProductScreenSPsList(cellPhoneServiceProjectIDs).ToList();
                if (insuranceProductScreenSPs.Any())
                {
                    for (int i = 0; i < insuranceProductScreenSPs.Count; i++)
                    {
                        if (!result)
                            return false;
                        var insuranceProductScreenSP = insuranceProductScreenSPs[i];
                        var entity = new InsuranceProductScreenSPsSnapshoot
                        {
                            InsuranceOrderServiceProjectId = insuranceProductScreenSP.CellPhoneServiceProjectID,
                            ServiceProjectType = (byte)serviceProjectType,
                            ProductTypeID=insuranceProductScreenSP.ProductTypeID,
                            AccessoriesTypeID = insuranceProductScreenSP.AccessoriesTypeID,
                            AccessoriesLevelID = insuranceProductScreenSP.AccessoriesLevelID
                        };
                        result = AddEntities(entity).ID > 0;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 获取可用配件快照
        /// </summary>
        /// <param name="insuranceOrderServiceProjectId">选择的是哪个服务项目</param>
        /// <param name="serviceProjectType">保险信息类型：和ServiceProductType是同一个枚举，根据ServiceProjectType去不同的表取数据，比如手机维修保险信息，就去CellPhoneInsuranceInfo表取</param>
        /// <returns></returns>
        public IQueryable<InsuranceProductScreenSPsSnapshoot> GetInsuranceProductScreenSPsSnapshoot(
            long insuranceOrderServiceProjectId, ServiceProductType serviceProjectType)
        {
            var bServiceProductType = (byte)serviceProjectType;
            return LoadEntities(o => o.InsuranceOrderServiceProjectId == insuranceOrderServiceProjectId && o.ServiceProjectType == bServiceProductType).AsNoTracking();
        }

    }
}